From 9565ddf794165e297acf511a108f9a9643ee615d Mon Sep 17 00:00:00 2001
From: it-fixcomart <it@fixcomart.co.id>
Date: Tue, 11 Jun 2024 13:40:23 +0700
Subject: <iman> update promotion program

---
 src/pages/shop/promo/[slug].tsx | 77 ++++++++++++++++++++++++++++++++---------
 1 file changed, 60 insertions(+), 17 deletions(-)

(limited to 'src/pages/shop/promo/[slug].tsx')

diff --git a/src/pages/shop/promo/[slug].tsx b/src/pages/shop/promo/[slug].tsx
index cd3e93c4..be5a715d 100644
--- a/src/pages/shop/promo/[slug].tsx
+++ b/src/pages/shop/promo/[slug].tsx
@@ -10,7 +10,6 @@ import { IPromotion } from '../../../../src-migrate/types/promotion'
 import React from 'react'
 import { SolrResponse } from "../../../../src-migrate/types/solr.ts";
 
-
 const BasicLayout = dynamic(() => import('../../../core/components/layouts/BasicLayout'))
 
 export default function PromoDetail() {
@@ -22,14 +21,15 @@ export default function PromoDetail() {
   const itemsPerPage = 12; // Jumlah item yang ingin ditampilkan per halaman
   const startIndex = (currentPage - 1) * itemsPerPage;
   const endIndex = Math.min(startIndex + itemsPerPage, promoData?.length || 0);
-  const visiblePromotions = promoData?.slice(startIndex, endIndex);
+  // const visiblePromotions = promoData?.slice(startIndex, endIndex);
   const [loading, setLoading] = useState(true); // Menambahkan status loading
+  const [fetchingData, setFetchingData] = useState(false)
 
 
   useEffect(() => {
     const loadPromo = async () => {
       try {
-        const items = await fetchPromoItemsSolr(Array.isArray(slug) ? slug[0] : slug)
+        const items = await fetchPromoItemsSolr(`type_value_s:${Array.isArray(slug) ? slug[0] : slug}`)
         console.log("slug sekarang ", slug)
         
         setPromoItems(items)
@@ -112,18 +112,60 @@ export default function PromoDetail() {
 
   console.log("data yg dikirim ke ProductPromoCard", promoData)
   function capitalizeFirstLetter(string) {
-    return string.charAt(0).toUpperCase() + string.slice(1);
+    // Ganti semua tanda _ dengan spasi
+    string = string.replace(/_/g, ' ');
+    
+    // Kapitalisasi huruf pertama setelah spasi atau awal string
+    return string.replace(/(^\w|\s\w)/g, function(match) {
+        return match.toUpperCase();
+    });
+
+
+  }
+
+  useEffect(() => {
+    const handleScroll = () => {
+      if (
+        !fetchingData &&
+        window.innerHeight + document.documentElement.scrollTop >= 0.95 * document.documentElement.offsetHeight
+      ) {
+        // User has scrolled to 95% of page height
+        
+        setTimeout(() => setFetchingData(true), 120);
+        setCurrentPage((prevPage) => prevPage + 1)
+      }
+    }
+
+    window.addEventListener('scroll', handleScroll)
+    return () => window.removeEventListener('scroll', handleScroll)
+  }, [fetchingData])
+
+  useEffect(() => {
+    if (fetchingData) {
+      // Fetch more data
+      // You may need to adjust this logic according to your API
+      fetchMoreData()
+    }
+  }, [fetchingData])
+
+  const fetchMoreData = async () => {
+    try {
+      // Add a delay of approximately 150ms
+      setTimeout(async () => {
+        // Fetch more data
+        // Update promoData state with the new data
+      }, 150)
+    } catch (error) {
+      console.error('Error fetching more data:', error)
+    } finally {
+      setTimeout(() => setFetchingData(false), 120);
+      
+    }
   }
 
-  const goToNextPage = () => {
-    setCurrentPage((prevPage) => prevPage + 1);
-    window.scrollTo({ top: 0, behavior: 'smooth' });
-  };
+  const visiblePromotions = promoData?.slice(0, currentPage * 12)
+
   
-  const goToPreviousPage = () => {
-    setCurrentPage((prevPage) => Math.max(prevPage - 1, 1));
-    window.scrollTo({ top: 0, behavior: 'smooth' });
-  };
   
   return (
     <BasicLayout>
@@ -142,14 +184,15 @@ export default function PromoDetail() {
           <div className='flex flex-wrap justify-center'>
             {visiblePromotions?.map((promotion) => (
               <div key={promotion.id} className="min-w-[40px] max-w-[400px] mr-[20px] mb-[20px] sm:w-full md:w-1/2 lg:w-1/3 xl:w-1/4">
-                <ProductPromoCard promotion={promotion} slug={Array.isArray(slug) ? slug[0] : slug} />
+                <ProductPromoCard promotion={promotion}/>
               </div>
             ))}
           </div>
-          <div className="flex justify-center mt-4">
-            <button onClick={goToPreviousPage} disabled={currentPage === 1} className="mr-2 px-4 py-2 bg-gray-200 rounded">Back</button>
-            <button onClick={goToNextPage} disabled={endIndex >= promoData.length} className="px-4 py-2 bg-gray-200 rounded">Next</button>
-          </div>
+          {fetchingData && (
+            <div className='container flex justify-center my-4'>
+              <LogoSpinner width={48} height={48} />
+            </div>
+          )}
         </>
       ) : (
         <div className="text-center my-8">
-- 
cgit v1.2.3

